*****************************************************************************
*			          Figures Descriptives with ESS	         				*
*																			*			
* Author: 			Valentina Gonzalez Rostani		        				*
* Contact: 			mag384@pitt.edu              							*
* Date: 			August 9 2024   										*
* Version:			Stata 17 												*                                                                          
*																			*
*****************************************************************************
/*
This do-file:
- Creates Table A11 using data from the ESS. 

Input:
- Data\Appendix_ESS.dta

Output:
- Table A11: Switching in Western European Countries from Mainstream Left to Outsider Radical Right parties 2002-2018 [Table\ESS_switching.tex]

*/

*Defining Directory
cd "C:\Users\vgonz\Dropbox\Pitt\OneDrive for Business\Dissertation - Vale\Paper 2 - Political-Economic Polarization\Replication"

*##########################################
* Load data
*##########################################
use "Data\Appendix_ESS.dta", clear 

*****************************************************************************
* Preparing variables
*****************************************************************************
{
// Generate a binary variable 'west2' to identify whether a country belongs to the specified list of Western countries. 
gen west2=(cou=="BEL" | cou=="DNK" | cou=="FIN" | cou=="FRA" | cou=="DEU" | cou=="GRC" | cou=="IRL" | cou=="NLD" | cou=="PRT" | cou=="ESP" | cou=="SWE" | cou=="GBR" | cou=="NOR" | cou=="CHE" | cou=="AUT" | cou=="ITA" | cou=="POL" )

// Generates a variable called switching comparing parfam and parfam_close. 
	* Parfam indicates the party family of the  party voted in the last election. 
	* parfam_close indicates the party family of the party the subject feel close now. 
	* Switching is defined as voted for another party but now feel close to a nationalist. 
gen switching2=.
replace switching2 =0 if parfam==parfam_close & parfam_close~=. & parfam~=.
replace switching2 =0 if parfam~=parfam_close & parfam_close~=70 
replace switching2 =1 if parfam~=70 & parfam_close==70 

// Generates a variable called switching comparing parfam and parfam_close. The difference with the one before is that this one just look at the left instead of any party.  
gen switching2_leftboth=.
replace switching2_leftboth =0 if parfam==parfam_close & parfam_close~=. & parfam~=.
replace switching2_leftboth =0 if parfam~=parfam_close & parfam_close~=70 
replace switching2_leftboth =1 if (parfam==30 | parfam==20 ) & (parfam_close==70 ) 


// Encode the string variable 'cntry' into a numeric variable 'country2' with value labels
encode cntry, gen(country2)

// Create a new variable 'countr_year' by multiplying 'country2' by 'year'. This creates a unique identifier for each country-year combination
gen countr_year=country2*year

}

// table A11: Switching in Western European Countries from Mainstream Left to Outsider Radical Right parties 2002-2018
{
eststo clear  // Clear any previously stored estimates

// Estimate logistic regression for 'switching2' with various predictors, storing the results. First results are swithing from any party. 
eststo:  logit switching2  meanprobfreyosborne  unemplindiv2 female agea mbtru2 rlgdgr    i.country2 i.year  if west2==1, cluster(countr_year)
eststo:  logit switching2  rti   unemplindiv2  female agea mbtru2 rlgdgr     i.country2 i.year if west2==1, cluster(countr_year)
eststo:  logit switching2  task3cog2and3   unemplindiv2  female agea mbtru2 rlgdgr i.country2 i.year  if west2==1, cluster(countr_year)

// Estimate logistic regression for 'switching2_leftboth' (switching from left parties to nationalist) with various predictors, storing the results
eststo:  logit switching2_leftboth  meanprobfreyosborne  unemplindiv2 female agea mbtru2 rlgdgr     i.country2 i.year   if west2==1, cluster(countr_year)
eststo:  logit switching2_leftboth  rti  unemplindiv2   female agea  mbtru2 rlgdgr    i.country2 i.year  if west2==1 , cluster(countr_year)
eststo:  logit switching2_leftboth  task3cog2and3  unemplindiv2    female agea mbtru2 rlgdgr     i.country2 i.year  if west2==1 , cluster(countr_year)

// Generate an output table with specific variables, saving it in the current results
   esttab 	///
		, keep(meanprobfreyosborne task3cog2and3 rti) replace label se compress nogap star(* 0.1 ** 0.05 *** 0.01) ///
		b(%6.3f) scalars( "N Observations"  )  	///
		indicate("Demographics = *male*" "Socio-econ = *mbtru2*"   "Country FE = *country*" "Year FE = *year", label($\checkmark$ ))	///
		noconstant nonotes nomtitles nodepvars
		

   esttab using "Table\ESS_switching.tex", ///
		 keep(meanprobfreyosborne task3cog2and3 rti) replace label se compress nogap star(* 0.1 ** 0.05 *** 0.01) ///
		b(%6.3f) scalars( "N Observations"  )  	///
		indicate("Demographics = *male*" "Socio-econ = *mbtru2*"   "Country FE = *country*" "Year FE = *year", label($\checkmark$ ))	///
		noconstant nonotes nomtitles nodepvars

}